package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Company;
import com.ssh.entity.Employee;
import com.ssh.entity.Position;
import com.ssh.manager.EmployeeManager;
import com.ssh.manager.PositionManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/employee/employee.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.userName,page\\.pageData\\[\\d+\\]\\.position\\.id,page\\.pageData\\[\\d+\\]\\.position\\.name," +
"page\\.pageData\\[\\d+\\]\\.employee\\.id,page\\.pageData\\[\\d+\\]\\.employee\\.userName,page\\.pageData\\[\\d+\\]\\.createDate"}),
@Result(name="getAll", type="json", params={"root","employees","excludeproperties","employees\\[\\d+\\]\\.id,employees\\[\\d+\\]\\.userName,employees\\[\\d+\\]\\.position\\.name"}),
@Result(name="update", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="verificationPwd", type="json", params={"root","jr","excludeproperties","jr\\.code"})
})
public class EmployeeAction extends ActionSupport{
	@Resource(name="employeeManagerImpl")
	private EmployeeManager employeeManager;
	@Resource(name="positionManagerImpl")
	private PositionManager positionManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private Position position;
	private Employee employee;
	private Company company;
	private List<Employee> employees;
	private int isUpdatePwd;
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public int getIsUpdatePwd() {
		return isUpdatePwd;
	}
	public void setIsUpdatePwd(int isUpdatePwd) {
		this.isUpdatePwd = isUpdatePwd;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public List<Employee> getEmployees() {
		return employees;
	}
	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}
	public Position getPosition() {
		return position;
	}
	public void setPosition(Position position) {
		this.position = position;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	
	/**
	 * 前往JSP
	 * @return
	 */
	public String goEmployeeJsp(){
		return "success";
	}
	
	public String findList(){
		//page.setNumberPerPage(20);
		Map conditionMap = new HashMap();
		conditionMap.put("position", position.getId());
		conditionMap.put("pid", employee.getUserName());
		employeeManager.findListByPage(conditionMap, page, isDesc);
		return "findList";
	}
	
	public String add(){
		position = positionManager.getById(position.getId());
		if (company!=null && company.getId()!=null) {
			employee.setEmployee(employeeManager.getById(company.getId()));
		}else {
			employee.setEmployee(null);
		}
		
		employee.setDel(0);
		employee.setCreateDate(new Timestamp(System.currentTimeMillis()));
		employee.setPosition(position);
		employeeManager.saveUpdate(employee);
		position = new Position();
		return "success";
	}
	
	public String update(){
		try {
			Employee e = employeeManager.getById(employee.getId());
			if(isUpdatePwd==1){
				e.setPassword(employee.getPassword());
			}
			if (position.getId() != e.getPosition().getId()) {
				position = positionManager.getById(position.getId());
				e.setPosition(position);
			}
			if(company!=null && company.getId()!=null){
				e.setEmployee(employeeManager.getById(company.getId()));
			}else {
				e.setEmployee(null);
			}
			
			employeeManager.saveUpdate(e);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("业务员信息修改失败", LogName.API);
			e.printStackTrace();
		}
		return "update";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					Employee e = employeeManager.getById(new Long(d[i]));
					e.setDel(1);
					employeeManager.saveUpdate(e);
					
					List<Employee> es = employeeManager.getAllByEid(e.getId());
					for (int j = 0; j < es.size(); j++) {
						es.get(j).setEmployee(null);
						employeeManager.saveUpdate(es.get(j));
					}
				}
			}else{
				Employee e = employeeManager.getById(employee.getId());
				e.setDel(1);
				employeeManager.saveUpdate(e);
				List<Employee> es = employeeManager.getAllByEid(e.getId());
				for (int j = 0; j < es.size(); j++) {
					es.get(j).setEmployee(null);
					employeeManager.saveUpdate(es.get(j));
				}
			}
			
			int _p = (int) page.getCurPageNum();
			
			page.setNumberPerPage(20);
			Map conditionMap = new HashMap();
			conditionMap.put("position", position.getId());
			conditionMap.put("pid", employee.getUserName());
			employeeManager.findListByPage(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("业务员删除失败", LogName.API);
			e.printStackTrace();
		}
		return "delete";
	}
	
	public String getAll(){
		employees = employeeManager.getAll();
		return "getAll";
	}
	
	public String verificationPwd(){
		String pwd = employee.getPassword();
		employee = employeeManager.getById(employee.getId());
		if (pwd.equals(employee.getPassword())) {
			jr.setCode("y");
		}else{
			jr.setCode("n");
		}
		return "verificationPwd";
	}
}
